package com.hxy.springboot06.interceptor;

import com.hxy.springboot06.bean.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {

    public boolean preHandler(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception
    {
      HttpSession session=request.getSession();
     User user=(User) session.getAttribute("user");
      if(user!=null){
         return true;
      }
      request.setAttribute("msg","您未登录，请先登录！");
      request.getRequestDispatcher("/").forward(request,response);
      return false;
    }
}
